package org.sith.taskrunner.gwt.client.view;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import org.sith.taskrunner.gwt.client.resources.TaskRunnerMessages;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;

public class UserTableWidget extends TableWidget<UserEntityProxy> implements IUserTableView{

    private static final TaskRunnerMessages MESSAGES = TaskRunnerMessages.App.getInstance();

    private Presenter presenter;




    @Override
    public Widget getViewWidget() {
        return this;
    }

    protected void configureColumns(CellTable<UserEntityProxy> cellTable) {
        TextColumn<UserEntityProxy> loginColumn = new TextColumn<UserEntityProxy>() {
            @Override
            public String getValue(UserEntityProxy object) {
                return object.getLogin();
            }
        };
        TextColumn<UserEntityProxy> roleColumn = new TextColumn<UserEntityProxy>() {
            @Override
            public String getValue(UserEntityProxy object) {
                return object.getRole().name();
            }
        };
        TextColumn<UserEntityProxy> creationDateColumn = new TextColumn<UserEntityProxy>() {
            @Override
            public String getValue(UserEntityProxy object) {
                return object.getCreationDate().toString();
            }
        };
        TextColumn<UserEntityProxy> isActiveColumn = new TextColumn<UserEntityProxy>() {

            @Override
            public String getValue(UserEntityProxy object) {
                return object.getActive() ? MESSAGES.activeColumnLabel() : MESSAGES.notActiveColumnLabel();
            }
        };

        cellTable.addColumn(loginColumn, MESSAGES.userNameTableHeader());
        cellTable.addColumn(roleColumn, MESSAGES.roleTableHeader());
        cellTable.addColumn(creationDateColumn, MESSAGES.creationDateTableHeader());
        cellTable.addColumn(isActiveColumn, MESSAGES.isActiveTableHeader());

    }

    @Override
    protected void addControlsToTable(HasWidgets widget) {
        Button reloadUsersButton = new Button(MESSAGES.reloadUsersLabel());
        reloadUsersButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                       presenter.reloadUsers();
            }
        });
        widget.add(reloadUsersButton);

        Button addUserButton = new Button(MESSAGES.addUserLabel());
        addUserButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                presenter.addUser();
            }
        });
        widget.add(addUserButton);


        Button updateUserButton = new Button(MESSAGES.updateUserLabel());
        updateUserButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                presenter.editUser(getSelectedObject());
            }
        });
        widget.add(updateUserButton);


           Button removeUserButton = new Button(MESSAGES.removeUserLabel());
        removeUserButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                presenter.removeUser(getSelectedObject());
            }
        });
        widget.add(removeUserButton);




    }

    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }
}
